package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.reader.enity.Book;
import com.imooc.reader.enity.Evaluation;
import com.imooc.reader.enity.Member;
import com.imooc.reader.mapper.BookMapper;
import com.imooc.reader.mapper.EvaluationMapper;
import com.imooc.reader.mapper.MemberMapper;
import com.imooc.reader.service.EvaluationService;
import com.imooc.reader.service.exception.BussinessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service("evaluationService")
// 事务注解
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class EvaluationServiceImpl implements EvaluationService {

    @Resource
    EvaluationMapper evaluationMapper;
    @Resource
    MemberMapper memberMapper;
    @Resource
    BookMapper bookMapper;

    /**
     * 查询书籍对应评论
     * @param bookId
     * @return
     */
    @Override
    public List<Evaluation> selectByBookId(Long bookId) {
        Book book = bookMapper.selectById(bookId);
        List<Evaluation> list = evaluationMapper.selectList(new QueryWrapper<Evaluation>().eq("book_id", bookId).eq("state","enable").orderByDesc("create_time"));
        for (Evaluation e:list
             ) {
            Member member = memberMapper.selectById(e.getMemberId());
            e.setBook(book);
            e.setMember(member);
        }
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<Evaluation> paging(Integer page, Integer limit) {
        Page evaluationPage = new Page(page, limit);
        QueryWrapper<Evaluation> evaluationQueryWrapper = new QueryWrapper<>();
        evaluationQueryWrapper.eq("state","enable");
        evaluationQueryWrapper.orderByDesc("create_time");
        IPage<Evaluation> pageObject = evaluationMapper.selectPage(evaluationPage, evaluationQueryWrapper);
        List<Evaluation> evaluationList = pageObject.getRecords();
        for (Evaluation e:evaluationList
             ) {
            Book book = bookMapper.selectById(e.getBookId());
            Member member = memberMapper.selectById(e.getMemberId());
            e.setMember(member);
            e.setBook(book);
        }
        return pageObject;
    }

    /**
     * 禁用短语
     * @param evaluationId
     * @param reason
     */
    @Override
    @Transactional
    public void updateEvaluationState(Long evaluationId, String reason) {
        Evaluation evaluation = evaluationMapper.selectById(evaluationId);
        evaluation.setDisableReason(reason);
        evaluation.setState("disable");
        int i = evaluationMapper.updateById(evaluation);
        if (i<=0){
            throw new BussinessException("E01","禁用失败");
        }
    }

}
